#pragma once
#include <pthread.h>
#include <iostream>
#include <string>
#include <cstdio>
#include <string>

typedef void *(*fun_t)(void *);
struct ThreadData // 线程执行的数据
{
    std::string name_; // 线程名字
    void *args_;       // 任务参数
};
class Thread
{
public:
    Thread(int num, fun_t callback, void *args):func_(callback)//出错原因，未初始化
    {
        char threadname[128];
        snprintf(threadname, sizeof(threadname), "thread_%d", num);
        name_ = threadname;
        tdata_.args_ = args;
        tdata_.name_ = name_;
    }
    ~Thread()
    {
    }
    void Start() // 启动线程
    {
        pthread_create(&tid_, nullptr, func_, (void *)&tdata_);
    }
    void Join() // 线程等待
    {
        pthread_join(tid_, nullptr);
    }
    std::string name()
    {
        return name_;
    }
    const pthread_t &tid()
    {
        return tid_;
    }
private:
    std::string name_; // 线程名
    pthread_t tid_;    // 线程id
    fun_t func_;       // 线程任务
    ThreadData tdata_; // 任务数据
};
